package com.zrq.myapplication;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zrq.myapplication.model.Phone;
import com.zrq.myapplication.mvp.Impl.MainPresenter;
import com.zrq.myapplication.mvp.MvpMainView;

public class MainActivity extends AppCompatActivity implements OnClickListener, MvpMainView {

    EditText input_Phone;
    Button btn_search;
    TextView result_phone;
    TextView result_province;
    TextView result_type;
    TextView result_carrier;
    //view的掌控者
    MainPresenter mainPresenter;
    //用于加载进度的Diolog
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input_Phone = (EditText) findViewById(R.id.input_phone);
        btn_search = (Button) findViewById(R.id.btn_search);
        result_phone = (TextView) findViewById(R.id.result_phone);
        result_province = (TextView) findViewById(R.id.result_province);
        result_type = (TextView) findViewById(R.id.result_type);
        result_carrier = (TextView) findViewById(R.id.result_carrier);

        btn_search.setOnClickListener(this);


        /**
         * presenter绑定当前视图
         */
        mainPresenter = new MainPresenter(this);
        mainPresenter.attach(this);

    }

    @Override
    public void onClick(View v) {

        mainPresenter.searchPhoneInfo(input_Phone.getText().toString());
    }

    /**
     * loadingView接口，显示与隐藏 ProgressDialog
     */
    @Override
    public void showLoading() {
        if (progressDialog == null) {
            progressDialog = ProgressDialog.show(this, "", "正在加载...", true,false);
        } else if (progressDialog.isShowing()) {
            progressDialog.setTitle("");
            progressDialog.setMessage("正在加载...");
        }
        progressDialog.show();
    }

    @Override
    public void hideLoading() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    /**
     * 主页面view的接口方法实现，用于提示用户以及更新页面
     */
    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    //
    @Override
    public void updateView() {
        Phone phone = mainPresenter.getPhoneInfo();
        Log.e("TAG", "phone: " + phone.getTelString());
        result_phone.setText("手机号码：" + phone.getTelString());
        result_province.setText("省份：" + phone.getProvince());
        result_type.setText("运营商：" + phone.getCatName());
        result_carrier.setText("归属运营商：" + phone.getCarrier());
    }
}
